<template>
	<view class="">
		<!-- 
			1.type:  group社群  
			groupPrice  discount_price  优惠券金额	
						
			2.type: activity活动
			iSVip 是否会员    vipPrice    normalPrice    discount_price  优惠券金额		 
		-->
		<u-popup round="30rpx" :show="btmShow" mode="bottom" @close="closePop" @open="">
			<view class="btmPop_wrapper">
				<!-- {{JSON.stringify(dataDic)}} -->
				<!-- dataDic.groupPrice -->
				<!-- {{payMoney}} -->
				<view class="name" v-if="dataDic.type == 'group'"> ¥{{payMoney}} </view>
				<view class="type1" v-if="calculatPrice > 0">
					<view class="left">
						<image class="img" src="@/static/images/BX/payPic1.png"></image>
						<view class="title">会员优惠</view>
					</view>
					<view class="right">
						<text class="title">-¥{{calculatPrice}}</text>
					</view>
				</view>
				<view class="type1" @click="scoreClick" v-if="showScoreItem">
					<view class="left">
						<image class="img" src="@/static/images/BX/payPic2.png"></image>
						<view class="title">账户积分</view>
					</view>
					<view class="scoreRight" v-if="scoreTF > 0">
						<view class="scoreTitle1">-¥{{scoreToPrice}}</view>
						<view class="scoreTitle2">{{scoreTF}}积分抵扣</view>
					</view>
					<image class="jian" :class="{ 'arrowTransform': showScore==true, 'arrowTransformReturn': showScore==false}"
						mode="widthFix" src="/static/images/BX/payPic3.png"></image>
				</view>
				<view class="type1" v-if="showScore">
					<view class="TF">
						<input v-model="scoreTF" type="number" @blur='blur' placeholder="请输入使用积分" />
					</view>
				</view>
				<view class="type1 typewarn">
					<view class="warnAlert">账户总积分{{coinDic.coin_now}}，10积分=1元</view>
				</view>
				<view class="payBtn" @click="payClick">确认付款</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scoreTF: 0,
				btmShow: false,
				dataDic: {
					type: '',
					groupPrice: '',
					discount_price: '',
					iSVip: '',
					vipPrice: '',
					normalPrice: '',
					discount_price: '',
					itemDic: {}, //传递过来的上级页面整体的对象，后续拓展使用
				},
				showScoreItem: true, //是否展示账户积分的条目， 优惠券>金额的时候不显示  全部抵扣了
				showScore: false, //是否展示积分
				maxScore: '', //限制到的最高可以使用的积分
				useScore: '', //可以使用的积分金额上线
				coinDic: {}
			}
		},
		onLoad() {

		},
		props: {

		},
		computed: {
			payMoney() {
				let groupPrice = parseFloat(this.dataDic.groupPrice)
				let discount_price = parseFloat(this.dataDic.discount_price)
				let scorePrice = 0
				console.warn(typeof(this.scoreTF), this.scoreTF)
				
				if (this.scoreTF.length > 0) {
					
					console.error(this.scoreTF)
					
					scorePrice = parseInt(this.scoreTF) / 10
					
				}
				console.log(this.dataDic.groupPrice)
				
				console.log(parseFloat(this.calculatPrice))
				console.log(this.dataDic.groupPrice)
				
				
				let realPay = this.dataDic.groupPrice - parseFloat(this.calculatPrice) - scorePrice
				if (realPay < 0) {
					realPay = 0
				}
				return realPay.toFixed(1)
			},
			// 优惠券需要展示的价格
			calculatPrice() {
				let groupPrice = parseFloat(this.dataDic.groupPrice)
				let discount_price = parseFloat(this.dataDic.discount_price)				
				if (discount_price >= groupPrice) {
					return groupPrice
				} else {
					return discount_price
				}
			},
			scoreToPrice() {
				let toMoney = parseInt(this.scoreTF) / 10
				console.log(toMoney)
				return toMoney
			}
		},
		methods: {
			blur() {
				console.log(this.scoreTF)
				let scoreTF = parseInt(this.scoreTF)
				if (scoreTF > this.maxScore) {
					this.scoreTF = this.maxScore.toString()
					uni.showToast({
						title: `可用积分${this.maxScore}`,
						icon: 'none'
					})
				}
			},
			userCoin() {
				this.$http.get(this.$api.userCoin, {}).then(res => {
					console.log(res)
					this.coinDic = res.data
					/*
						1.type:  group社群  
							groupPrice  discount_price  优惠券金额	
						2.type: activity活动
							iSVip 是否会员    vipPrice    normalPrice    discount_price  优惠券金额		 
					*/
					if (this.dataDic.type == 'group') {
						let discount_price = parseFloat(this.dataDic.discount_price)
						let groupPrice = parseFloat(this.dataDic.groupPrice)
						console.log(this.dataDic)						
						console.log(discount_price)
						console.log(groupPrice)
						
						// debugger
						if (groupPrice > discount_price) {
							this.showScoreItem = true
							let shengyuPrice = groupPrice - parseFloat(this.calculatPrice)
							let shengyuScore = (shengyuPrice * 10).toFixed(0) //剩余的金额对应的积分
							
							console.log(shengyuPrice)
							console.log(shengyuScore)
							console.log(this.coinDic)
							
							// debugger
							// 输入完成后最高限制的积分
							if (shengyuScore > this.coinDic.coin_now) {
								this.maxScore = this.coinDic.coin_now
							} else {
								this.maxScore = shengyuScore
							}
							this.scoreTF = this.maxScore.toString() //智能匹配为最高可以使用的积分
							console.log(this.maxScore)
							// debugger
							
						} else {
							this.showScoreItem = false
							this.scoreTF = '0'
						}
					}
				})
			},
			scoreClick() {
				this.showScore = !this.showScore
			},
			open(item) {
				this.btmShow = true
				console.log(item)
				this.dataDic = item
				this.userCoin()
			},
			close() {
				this.btmShow = false
			},
			payClick() {
				this.btmShow = false
				// //item对象  输入的积分  实际付款的金额
				this.$emit('toWechatPay', this.dataDic.itemDic, this.scoreTF, this.payMoney) 
			},
			closePop() {
				this.btmShow = false
				this.$emit('closePop', '')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.index {
		background: $UNI-backgroundColor;
	}

	.btmPop_wrapper {
		border-radius: 80rpx;
		overflow: hidden;
		background: white;
		min-height: 760rpx;
		padding: 30rpx;

		.name {
			color: #252525;
			font-size: 62rpx;
			font-weight: bold;
			text-align: center;
		}

		.payBtn {
			background: linear-gradient(180deg, #F88328 0%, #FF4945 100%);
			color: white;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-radius: 100rpx;
			margin-top: 80rpx;
			margin-bottom: 50rpx;
		}

		.typewarn {
			padding-top: 10rpx !important;
		}

		.type1 {
			background: $UNI-backgroundColor;
			padding: 25rpx 30rpx;
			color: #252525;
			font-size: 33rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.warnAlert {
				color: #252525;
				font-size: 26rpx;
			}

			.TF {
				background: white;
				padding: 25rpx 20rpx;
				border-radius: 10rpx;
				width: 100%;
				border-radius: 80rpx;
			}

			.left {
				display: flex;
				align-items: center;

				.img {
					width: 56rpx;
					height: 56rpx;
				}

				.title {
					padding-left: 30rpx;
					color: #252525;
					font-size: 29rpx;
				}
			}

			.scoreRight {
				flex: 1;
				padding-right: 20rpx;
				text-align: right;

				.scoreTitle1 {
					color: #FE4A28;
					font-size: 28rpx;
				}

				.scoreTitle2 {
					color: #575757;
					font-size: 28rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;

				.title {
					color: #FE4A28;
					font-size: 29rpx;
					padding-right: 10rpx;
				}
			}

		}

		.type2 {
			margin-top: 20rpx;
			background: white;
			padding: 20rpx 30rpx;
			color: #E02021;
			font-size: 33rpx;
		}
	}

	.arrowTransform {
		transition: 0.2s;
		transform-origin: center;
		transform: rotateZ(-90deg);
	}

	.arrowTransformReturn {
		transition: 0.2s;
		transform-origin: center;
		transform: rotateZ(0deg);
	}

	.jian {
		width: 33rpx;
		height: 33rpx;
	}
</style>